package com.prd.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.prd.config.LoginInterceptor;
import com.prd.dto.SysPositionDTO;
import com.prd.model.SysPositionDO;
import com.prd.mapper.SysPositionMapper;
import com.prd.service.SysPositionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.util.CommonUtil;
import com.prd.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 部门职务表 服务实现类
 * </p>
 *
 * @author lizhaoyi
 * @since 2024-01-04
 */
@Service
public class SysPositionServiceImpl extends ServiceImpl<SysPositionMapper, SysPositionDO> implements SysPositionService {

    @Autowired
    private SysPositionMapper sysPositionMapper;

    /**
     * 添加部门职务
     * @param sysPositionDTO
     * @return
     */
    @Override
    public JsonData add(SysPositionDTO sysPositionDTO) {

        SysPositionDO sysPositionDO = sysPositionMapper.selectOne(new LambdaQueryWrapper<SysPositionDO>().eq(SysPositionDO::getPosition, sysPositionDTO.getPosition()));
        if (sysPositionDO != null) {
            return JsonData.buildError("部门职务已存在。");
        }
        sysPositionDTO.setCreateBy(LoginInterceptor.threadLocal.get().getName());
        sysPositionDTO.setCreatedTime(LocalDateTime.now());

        sysPositionMapper.insert(CommonUtil.dtoTransfer(sysPositionDTO, SysPositionDO.class));

        return JsonData.buildSuccess();
    }

    /**
     * 编辑
     * @param sysPositionDTO
     * @return
     */
    @Override
    public JsonData edit(SysPositionDTO sysPositionDTO) {

        sysPositionMapper.updateById(CommonUtil.dtoTransfer(sysPositionDTO, SysPositionDO.class));

        return JsonData.buildSuccess();
    }

    /**
     * 移除
     * @param ids
     * @return
     */
    @Override
    public JsonData deleteByIds(List<String> ids) {

        sysPositionMapper.deleteBatchIds(ids);

        return JsonData.buildSuccess();
    }

    /**
     * 查询部门职务列表
     * @return
     */
    @Override
    public JsonData selectList() {
        return JsonData.buildSuccess(sysPositionMapper.selectList(new LambdaQueryWrapper<SysPositionDO>().orderBy(false,false,SysPositionDO::getCreatedTime)));
    }

    /**
     * 查询部门职务
     * @param id
     * @return
     */
    @Override
    public JsonData selectOne(Long id) {
        return JsonData.buildSuccess(sysPositionMapper.selectById(id));
    }
}
